package cn.hengzhu.main_manager.service.impl;

import cn.hengzhu.main_manager.repository.dao.ManageCaseTypeMapper;
import cn.hengzhu.main_manager.repository.dao.ManageCaseTypePriceMapper;
import cn.hengzhu.main_manager.repository.domain.ManageCaseType;
import cn.hengzhu.main_manager.repository.domain.ManageCaseTypePrice;
import cn.hengzhu.main_manager.repository.domain.ManageCaseTypePriceExample;
import cn.hengzhu.main_manager.repository.vo.ManagerCaseTypePriceVO;
import cn.hengzhu.main_manager.service.ManageCaseTypeService;
import cn.hengzhu.main_manager.utils.AmountUtils;
import cn.hengzhu.main_manager.utils.NumberHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 谭芸
 * @create 2021-03-29 11:34
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ManageCaseTypeServiceImpl implements ManageCaseTypeService {

    @Autowired
    private ManageCaseTypeMapper manageCaseTypeMapper;

    @Autowired
    private ManageCaseTypePriceMapper manageCaseTypePriceMapper;


    @Override
    public Boolean persistentSave(ManageCaseType manageCaseType, Map<String, ManageCaseTypePrice> managerCaseTypePriceMap) {
        Collection<ManageCaseTypePrice> list = managerCaseTypePriceMap.values();
        manageCaseType.setCrateTime(new Date());
        int state = manageCaseTypeMapper.insertReturnsTheId(manageCaseType);// 将收费规则名称等等插入
        if (state != 1){
            return false;
        }
        Integer caseTypeId = manageCaseType.getTypeId(); // 获得插入后的id
        Date date = new Date();
        int tag = 0;
        for (ManageCaseTypePrice manageCaseTypePrice : list) {
            manageCaseTypePrice.setCreateTime(date);
            manageCaseTypePrice.setCaseTypeId(caseTypeId);
            manageCaseTypePriceMapper.insert(manageCaseTypePrice);
            tag++;
        }
        return list.size() == tag;
    }

    @Override
    public List<ManageCaseTypePrice> selectByExample(ManageCaseTypePriceExample example) {
        List<ManageCaseTypePrice> list = manageCaseTypePriceMapper.selectByExample(example);
        for (ManageCaseTypePrice manageCaseTypePrice : list) {
            String thePrice = manageCaseTypePrice.getThePrice();
            try {
                thePrice = AmountUtils.changeF2Y(thePrice); // 分转为元
            }catch (Exception e){
                e.printStackTrace();
            }
            manageCaseTypePrice.setThePrice(thePrice);
        }
        return list;
    }


    /**
     * 删除收费类型
     * @param id 主键
     * @param caseTypeId 外键
     * @return
     */
    @Override
    public int persistentDel(String id, String caseTypeId) {
        ManageCaseTypePriceExample example = new ManageCaseTypePriceExample();
        example.createCriteria().andCaseTypeIdEqualTo(Integer.valueOf(caseTypeId)).andIdEqualTo(Integer.valueOf(id));
        return manageCaseTypePriceMapper.deleteExample(example);
    }

    @Override
    public Boolean deleteAllPrimaryKey(Integer typeId) {
        ManageCaseTypePriceExample example = new ManageCaseTypePriceExample();
        example.createCriteria().andCaseTypeIdEqualTo(typeId);
        List<ManageCaseTypePrice> list = manageCaseTypePriceMapper.selectByExample(example);

        int sign = 0;
        // 删除收费规则名称下的所有收费规则
        if (list.size() > 0){
            for (ManageCaseTypePrice manageCaseTypePrice : list) {
                int i = manageCaseTypePriceMapper.deleteByPrimaryKey(manageCaseTypePrice.getId());
                sign += i;
            }
        }
        int returnEd = manageCaseTypeMapper.deleteByPrimaryKey(typeId);
        if (sign == list.size() && returnEd == 1){
            return true;
        }
        return false;
    }

    /**
     * 修改数据库中的收费规则
     * @param mctp 数据
     * @param id 主键
     * @param caseTypeId 外键
     * @return
     */
    @Override
    public int persistentUpdate(ManagerCaseTypePriceVO mctp, String id, String caseTypeId) {
        ManageCaseTypePriceExample example = new ManageCaseTypePriceExample();
        example.createCriteria().andCaseTypeIdEqualTo(Integer.valueOf(caseTypeId)).andIdEqualTo(Integer.valueOf(id));
        String thePrice = mctp.getThePrice();
        mctp.setThePrice(AmountUtils.changeY2F(thePrice)); // 元转为分 并存入数据库
        return manageCaseTypePriceMapper.updateExample(mctp, example);
    }

    @Override
    public int persistentInsert(ManagerCaseTypePriceVO mctp, String typeId) {
        ManageCaseTypePrice manageCaseTypePrice = new ManageCaseTypePrice();
        manageCaseTypePrice.setCreateTime(new Date());
        manageCaseTypePrice.setCaseTypeId(Integer.valueOf(typeId));
        manageCaseTypePrice.setTheLenght(mctp.getTheLenght());
        String thePrice = mctp.getThePrice();
        thePrice = AmountUtils.changeY2F(thePrice); // 转为分
        manageCaseTypePrice.setThePrice(thePrice);
        return manageCaseTypePriceMapper.insert(manageCaseTypePrice);
    }



}
